package org.gm.mybatis.ext.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;


public class UUIDTypeHandler extends BaseTypeHandler<String> {
	@Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter,
            JdbcType jdbcType) throws SQLException {
        
    	ps.setObject(i, UUID.fromString(parameter));
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
    	if(rs.getObject(columnName)==null){
    		return "";
    	}
    	return ((UUID)rs.getObject(columnName)).toString();
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    	if(rs.getObject(columnIndex)==null){
    		return "";
    	}
    	return ((UUID)rs.getObject(columnIndex)).toString();
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
    	if(cs.getObject(columnIndex)==null){
    		return "";
    	}
    	return ((UUID)cs.getObject(columnIndex)).toString();
    }
}
